﻿using System;
using System.Linq;
using System.Data;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Data.OracleClient;
using Models;
using Utility;
using Service.Interface;
using Repository.Interface;
using SqlSugar;
using System.Threading.Tasks;

namespace Service.Impl
{
    public class FlowServices : IFlowServices
    {
        private IBaseRepository _PackinforRepository;
        private SqlSugarClient _db;
        public FlowServices(ISqlSugarClient db, IBaseRepository PackinforRepository)
        {
            _db = db as SqlSugarClient;
            _PackinforRepository = PackinforRepository;
        }
        public List<ProcessFlow> GetProcessFlowByProduct(ProcessFlow flow)
        {
            string sql = $@"SELECT PROCESS_NAME AS OPCODE,PROCESS_NAME AS OPRDESC,PROCESS_SEQ AS OPRSEQ FROM  PROCESS_INFO 
                            WHERE PRODUCT='{flow.ProductionType_c}' ORDER BY PROCESS_SEQ";
            DataTable dt = new DataTable();
            if (flow.ProductionType_c.Contains("OFN"))
                dt = _db.GetConnection("SAE_OFN").Ado.GetDataTable(sql.Replace(@"[\r\n]", "")); 
            else
                dt = _db.GetConnection("OUSB").Ado.GetDataTable(sql.Replace(@"[\r\n]", ""));
            List<ProcessFlow> lstProcessFlow = new List<ProcessFlow>();
            foreach (DataRow dr in dt.Rows)
            {
                ProcessFlow processFlow = new ProcessFlow();
                processFlow.Company = flow.Company;
                processFlow.PartNum = flow.PartNum;
                processFlow.OprSeq = Convert.ToInt32(dr["OprSeq"]);
                processFlow.OpCode = Convert.ToString(dr["OpCode"]);
                processFlow.OprDesc = Convert.ToString(dr["OprDesc"]);
                processFlow.ProductionType_c = flow.ProductionType_c;
                processFlow.IsRework_c = flow.IsRework_c;
                lstProcessFlow.Add(processFlow);
            }
            return lstProcessFlow;
        }

        public List<object> GetProcessName()
        {
            string strsql = @"select distinct(process_name) from process_info";
            var data = _db.GetConnection("OUSB").Ado.SqlQuery<string[]>(strsql);
            List<object> result = data.Select(x => new { process_name = x[0] }).Cast<object>().ToList();
            return result;
        }

        public List<object> GetReworkCategory(string product)
        {
            string strsql = $@"SELECT DISTINCT CATEGORY,CATEGORY_VALUE from REWORK_CATEGORY_LIST { (string.IsNullOrEmpty(product) ? "" : $"where PRODUCT ='{product}'")}";
            var data = _db.GetConnection("OUSB").Ado.SqlQuery<string[]>(strsql);
            List<object> result = data.Select(x => new { Category = x[0], CategoryValue = x[1] }).Cast<object>().ToList();
            return result;
        }

        public bool SynEquipment<TEntity>(List<TEntity> entities)
        {
            List<EquipmentInfo> equipment = entities as List<EquipmentInfo>;
            var x = _db.GetConnection("OUSB").Storageable(equipment)
                       .SplitUpdate(t => t.Any(m => m.Id == t.Item.Id))
                       .SplitInsert(t => !t.Any(m => m.Id == t.Item.Id))
                       .ToStorage();
            x.AsUpdateable.ExecuteCommand();//存在更新
            x.AsInsertable.ExecuteCommand();//不存在插入
            return false;
        }

    }
}
